package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.List;

@Service
@Transactional
public class EmployeeServiceImpl implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;


    @Override
    public void delete(Long id) {

        employeeMapper.deleteByPrimaryKey(id);

    }

    @Override
    public void save(Employee employee, Long[] ids) {

        employeeMapper.insert(employee);

        if (ids != null && ids.length>0){

            //关联关系
            for (Long roleId : ids){

                employeeMapper.insertRelation(employee.getId(), roleId);
            }
        }
    }

    @Override
    public Employee get(Long id) {


        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Employee> listALL() {

        return employeeMapper.selectAll();
    }

    @Override
    public void update(Employee employee, Long[] ids) {

        employeeMapper.updateByPrimaryKey(employee);

        //删除关系
        employeeMapper.deleteRelation(employee.getId());

        if (ids != null && ids.length>0){

            //关联关系
            for (Long roleId : ids){

                employeeMapper.insertRelation(employee.getId(), roleId);
            }
        }
    }

    @Override
    public PageResult<Employee> query(QueryObject qo) {

        int count = employeeMapper.selectForCount(qo);
        if (count == 0){

            return new PageResult<>(qo.getCurrentPage(),qo.getPageSize() , Collections.emptyList(), count);

        }

        //获取总页数
        int totalPage = count % qo.getPageSize() == 0
                ? count / qo.getPageSize() : count / qo.getPageSize() + 1;
        //判断当前页是否大于总页数,如果是,把总页数作为当前页来查询分页结果集

        if (qo.getCurrentPage() > totalPage){

            qo.setCurrentPage(totalPage);
        }

        List<Employee> employees = employeeMapper.selectForList(qo);

        return new PageResult<>(qo.getCurrentPage(), qo.getPageSize(), employees, count);

    }

    @Override
    public Employee login(String username, String password) {

        //查询数据库中是否有匹配的数据
        if (!StringUtils.hasText(username)){

            throw new RuntimeException("用户名不能为空");
        }

        if (!StringUtils.hasText(password)){

            throw new RuntimeException("密码不能为空");
        }

        Employee employee = employeeMapper.selectByUsernameAndPassword(username, password);
        if (employee==null){

            throw new RuntimeException("用户名或密码错误");
        }

        return employee;
    }


    @Override
    public void updateAll(String oldPassword, String newPassword) {

        Employee employee = UserContext.getCurrentUser();
        System.out.println(employee.getPassword());
        //查询数据库中是否有匹配的数据
        if (oldPassword==null || oldPassword.trim().length()==0){

            throw new RuntimeException("旧密码不能为空");
        }

        if (newPassword==null || newPassword.trim().length()==0){

            throw new RuntimeException("新密码不能为空");
        }


        if (!oldPassword.equals(employee.getPassword())){

            System.out.println(oldPassword);

            throw new RuntimeException("原密码错误");
        }


        employee.setPassword(newPassword);

        employeeMapper.selectByoldPasswordAndnewPassword(employee);
    }

    @Override
    public void insert1(Employee employee) {

        employeeMapper.selectByoldPasswordAndnewPassword(employee);
    }

}
